<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category   Zeed
 * @package    Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright  Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author     Zeed Team (http://blog.zeed.com.cn)
 * @since      2016-09-12
 * @version    SVN: $Id: $
 */

class ClearTempUserController extends ShellAbstract
{
    const GROUP_TEMP = 11; // 定义临时用户组 ID
    const DELAYED_HOURS = 3; // 定义临时用户到期后延时多长时间进行数据清空。单位：小时
    const UPLOAD_URL = '/home/project/archive/upload'; // 定义附件存放地址

    /**
     * 定时清除临时用户数据
     */
    public function clear()
    {
        Zeed_Log::instance()->log('开始查询临时用户...', Zeed_Log::INFO);

        /* 处理时间条件 */
        $nowtime = time();
        
        try {
            /* 查询临时用户 */
            $where_tempuser = 'g.groupid = ' . self::GROUP_TEMP;
            $order_tempuser = 'ctime DESC';
            $cols_tempuser = array('userid', 'username', 'exptime');
            $user_temp = Admin_Model_User::instance()->getUsersByGroupid($where_tempuser, $order_tempuser, null, null, $cols_tempuser);

            Zeed_Log::instance()->log('查询到 ' . count($user_temp) . ' 个临时用户，开始检查有效期...', Zeed_Log::INFO);

            $nums_clear = 0;
            if (! empty($user_temp)) {
                foreach ($user_temp as $k => $v) {
                    $times_diff = $nowtime - strtotime($v['exptime']);
                    if ($times_diff > self::DELAYED_HOURS * 3600) {
                        Zeed_Log::instance()->log('ID 为 ' . $v['userid'] . ' 的临时用户有效期已过，开始清空数据...', Zeed_Log::INFO);

                        $this->_clearData((int) $v['userid'], $v['username']);
                        $nums_clear++;

                        Zeed_Log::instance()->log('ID 为 ' . $v['userid'] . ' 的临时用户数据已清空', Zeed_Log::INFO);
                    }
                }
            }

            Zeed_Log::instance()->log('处理完成，本次共处理 ' . $nums_clear . ' 个临时用户', Zeed_Log::INFO);
        } catch (Exception $e) {
            Zeed_Log::instance()->log('检查失败 : ' . $e->getMessage(), Zeed_Log::INFO);
        }
    }

    /**
     * 执行清除
     */
    private function _clearData($userid, $username)
    {
        /* 查询附件表中的文件编辑结果信息，即临时工作台信息 */
        $attachments = Attachment_Model_Content::instance()->fetchByFV('userid', $userid);

        /* 删除磁盘文件 */
        if (! empty($attachments)) {
            foreach ($attachments as $k => $v) {
                exec('rm -rf ' . self::UPLOAD_URL . $v['filepath']);
            }
        }

        /* 删除工作台信息 */
        Attachment_Model_Content::instance()->deleteByFV('userid', $userid);

        /* 删除下载历史信息 */
        Archive_Model_History_Download::instance()->deleteByFV('userid', $userid);

        /* 删除音视频文件打点记录 */
        Attachment_Model_Content_Dot::instance()->deleteByFV('userid', $userid);

        /* 删除授权案件信息 */
        Archive_Model_Share::instance()->deleteByFV('share_to', $userid);

        /* 删除用户权限信息 */
        $where_permission = "ptype='user' AND parameter='" . $username . "'";
        Admin_Model_User_Permission::instance()->delete($where_permission);

        /* 删除用户组关系信息 */
        Admin_Model_User_Group::instance()->deleteByPK($userid);

        /* 删除用户信息 */
        Admin_Model_User::instance()->deleteByPK($userid);
    }
}

// End ^ Native EOL ^ encoding
